package com.hckjsec.system.controller;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.common.utils.sign.Base64Converter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.system.domain.SysMatter;
import com.hckjsec.system.service.ISysMatterService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;
import com.hckjsec.common.utils.StringUtils;

/**
 * 公证事项Controller
 * 
 * @author cyt
 * @date 2021-08-02
 */
@RestController
@RequestMapping("/system/matter")
public class SysMatterController extends BaseController
{
    @Autowired
    private ISysMatterService sysMatterService;

    /**
     * 查询公证事项列表
     */
    @GetMapping("/list")
    public AjaxResult list(SysMatter sysMatter)
    {
        List<SysMatter> list = sysMatterService.selectSysMatterList(sysMatter);
        return AjaxResult.success(list);
    }

    /**
     * 导出公证事项列表
     */
    @Log(title = "公证事项", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysMatter sysMatter)
    {
        List<SysMatter> list = sysMatterService.selectSysMatterList(sysMatter);
        ExcelUtil<SysMatter> util = new ExcelUtil<SysMatter>(SysMatter.class);
        return util.exportExcel(list, "matter");
    }

    /**
     * 获取公证事项详细信息
     */
    @GetMapping(value = "/{matterId}")
    public AjaxResult getInfo(@PathVariable("matterId") Long matterId)
    {
        return AjaxResult.success(sysMatterService.selectSysMatterById(matterId));
    }

    /**
     * 新增公证事项
     */
    @Log(title = "公证事项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMatter sysMatter)
    {
       String matterNumber="";
       if(sysMatter.getMatterId() == 0){//代表添加一级事项
           sysMatter.setParentId(0L);
           List<SysMatter> SysMatterList=sysMatterService.selectSysMatterByIdLength("4");
           List<Integer> matterNumberList= new ArrayList<Integer>();
           SysMatterList.forEach(sysMatter1 -> {
               matterNumberList.add(Integer.parseInt(sysMatter1.getMatterNumber()));
           });
           Integer max = matterNumberList.stream().reduce(Integer::max).get();
           String str = String.format("%04d", (max+1));
           matterNumber=str;
       }else{
           sysMatter.setParentId(sysMatter.getMatterId());
           //1.根据id得到编号
           SysMatter sysMatterNumber=sysMatterService.selectSysMatterById(sysMatter.getMatterId());
           //2.得到list
           List<SysMatter> SysMatterList=sysMatterService.selectSysMatterByNumber(sysMatterNumber.getMatterNumber());
           if (SysMatterList.size() == 1){//无子事项直接添加
               matterNumber=SysMatterList.get(0).getMatterNumber()+"01";
           }else{
               List<Integer> matterNumberList= new ArrayList<Integer>();
               SysMatterList.forEach(sysMatter1 -> {
                   String oldMatterNumber="";
                   if(sysMatter1.getMatterNumber().length() != sysMatterNumber.getMatterNumber().length()){
                       oldMatterNumber=sysMatter1.getMatterNumber().substring(sysMatterNumber.getMatterNumber().length());
                       matterNumberList.add(Integer.parseInt(oldMatterNumber));
                   }

               });
               Integer max = matterNumberList.stream().reduce(Integer::max).get();
               String str = SysMatterList.get(0).getMatterNumber()+String.format("%02d", (max+1));
               matterNumber=str;
           }
       }
        if (!"".equals(StringUtils.isHcNull(sysMatter.getRemarks(),""))){//解密
            sysMatter.setRemarks(Base64Converter.decode(sysMatter.getRemarks()));
        }else{
            sysMatter.setRemarks(sysMatter.getRemarks());
        }
        sysMatter.setMatterNumber(matterNumber);
        sysMatter.setCreateBy(SecurityUtils.getNickName());
        return toAjax(sysMatterService.insertSysMatter(sysMatter));
    }

    /**
     * 修改公证事项
     */
    @Log(title = "公证事项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMatter sysMatter)
    {
        if (!"".equals(StringUtils.isHcNull(sysMatter.getRemarks(),""))){//解密
            sysMatter.setRemarks(Base64Converter.decode(sysMatter.getRemarks()));
        }else{
            sysMatter.setRemarks(sysMatter.getRemarks());
        }
        sysMatter.setCreateBy(SecurityUtils.getNickName());
        return toAjax(sysMatterService.updateSysMatter(sysMatter));
    }

    /**
     * 删除公证事项
     */
    @Log(title = "公证事项", businessType = BusinessType.DELETE)
	@DeleteMapping("/{matterIds}")
    public AjaxResult remove(@PathVariable Long[] matterIds)
    {
        return toAjax(sysMatterService.deleteSysMatterByIds(matterIds));
    }
}
